package com.example.tellwin.mine.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseFragment;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.home.act.TeacherDetailActivity;
import com.example.tellwin.mine.adapter.TeacherAdapter;
import com.example.tellwin.mine.bean.AttentionTeacherBean;
import com.example.tellwin.mine.contract.AttentionTeacherContract;
import com.example.tellwin.mine.presenter.AttentionTeacherPresenter;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class AttentionTeacherFragment extends CpBaseFragment implements AttentionTeacherContract.View {


    @BindView(R.id.teacher_rv)
    RecyclerView teacherRv;
    Unbinder unbinder;

    private TeacherAdapter teacherAdapter;
    private List<AttentionTeacherBean>teacherBeans = new ArrayList<>();

    @Inject
    AttentionTeacherPresenter mPresenter;

    @Override
    public View onCreateRootView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_attention_teacher, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initEvent();
        initData();
        return view;
    }

    private void initView() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        teacherRv.setLayoutManager(linearLayoutManager);
        teacherRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if(position == 0){
                    outRect.top = getResources().getDimensionPixelOffset(R.dimen.dp_15);
                }
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_15);
            }
        });
        teacherAdapter = new TeacherAdapter();
        teacherRv.setAdapter(teacherAdapter);
    }

    private void initEvent() {
        teacherRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(CommonUtils.isSlideToBottom(teacherRv)) {
                    mPresenter.getAttentionTeacherList(true);
                }
            }
        });
        teacherAdapter.setOnTeacherItemClickListener(new TeacherAdapter.OnTeacherItemClickListener() {
            @Override
            public void onItemClick(int position, AttentionTeacherBean teacherBean) {
                Intent intent =new Intent(getActivity() , TeacherDetailActivity.class);
                intent.putExtra(Common.TEACHER_ID, teacherBean.getUserId());
                intent.putExtra(Common.TEACHER_NAME, teacherBean.getRealName());
                startActivity(intent);
            }

            @Override
            public void onItemCancelAttentionClick(int position, AttentionTeacherBean teacherBean) {
                mPresenter.attention(position,teacherBean.getUserId());
            }
        });
    }

    private void initData() {
        mPresenter.getAttentionTeacherList(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void attentionTeacherListResult(boolean isLoadMore, List<AttentionTeacherBean> attentionQuestionBeans) {
        if(isLoadMore){
            teacherBeans.addAll(attentionQuestionBeans);
            teacherAdapter.notifyDataSetChanged();
        }else {
            this.teacherBeans = attentionQuestionBeans;
            teacherAdapter.setTeacherBeans(teacherBeans);
        }
    }

    @Override
    public void attention(int position, String teacherId) {
        teacherBeans.remove(position);
        teacherAdapter.notifyDataSetChanged();
    }
}
